package com.softstao.guoyu.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseFragment;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.MeToOtherList;
import com.softstao.guoyu.mvp.interactor.me.RebateInteractor;
import com.softstao.guoyu.mvp.presenter.me.RebatePresenter;
import com.softstao.guoyu.mvp.viewer.me.MeToOtherListViewer;
import com.softstao.guoyu.ui.activity.me.RebateDetailActivity;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.EmptyLayout;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/20.
 */

public class MeToOtherListFragment extends BaseFragment implements MeToOtherListViewer {

    @AIPresenter(presenter = RebatePresenter.class, interactor = RebateInteractor.class)
    RebatePresenter presenter;

    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.detail_time)
    TextView detailTime;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @BindView(R.id.more)
    TextView more;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private int year, month;
    private Calendar calendar = Calendar.getInstance();
    private List<MeToOtherList.RebateListBean> rebateList = new ArrayList<>();
    private RecycleViewBaseAdapter<MeToOtherList.RebateListBean> adapter;

    @Override
    public int _ContentView() {
        return R.layout.fragment_rebate_list;
    }

    @Override
    public void initView() {
        setPtrFrameLayoutEnable();
        scrollView.setOnScrollChangedListener(this);

        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        time.setText(year + "年" + month + "月");
        detailTime.setText(year + "年" + month + "月返利明细");

        adapter = new RecycleViewBaseAdapter<MeToOtherList.RebateListBean>(rebateList, R.layout.layout_rebate_list_item) {
            @Override
            public void convert(RecycleViewHolder holder, MeToOtherList.RebateListBean rebateListBean) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                if (rebateListBean.getAvatar() != null && !rebateListBean.getAvatar().equals("")) {
                    Glide.with(mContext).load(rebateListBean.getAvatar()).into((ImageView) holder.getView(R.id.avatar));
                } else {
                    ((ImageView) holder.getView(R.id.avatar)).setImageResource(R.mipmap.default_avatar);
                }
                holder.setText(R.id.name, rebateListBean.getName())
                        .setText(R.id.level, rebateListBean.getLevel())
                        .setText(R.id.mobile, rebateListBean.getMobile())
                        .setText(R.id.money, getResources().getString(R.string.rmb) + LZUtils.priceFormat(rebateListBean.getMoney()));
            }
        };
        adapter.setListener(position -> {
            Intent intent = new Intent(mContext, RebateDetailActivity.class);
            intent.putExtra("id", rebateList.get(position).getSubAgentId());
            intent.putExtra("type", 2);
            intent.putExtra("month", time.getText());
            startActivity(intent);
        });
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(mContext));
    }

    @OnClick({R.id.left, R.id.right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left:
                month--;
                if (month < 1) {
                    year--;
                    month = 12;
                }
                time.setText(year + "年" + month + "月");
                detailTime.setText(year + "年" + month + "月返利明细");
                currentPage = 1;
                findMeToOtherList();
                break;
            case R.id.right:
                month++;
                if (month > 12) {
                    year++;
                    month = 1;
                }
                time.setText(year + "年" + month + "月");
                detailTime.setText(year + "年" + month + "月返利明细");
                currentPage = 1;
                findMeToOtherList();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        findMeToOtherList();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        findMeToOtherList();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        findMeToOtherList();
    }

    @Override
    public void findMeToOtherList() {
        loading.setVisibility(View.VISIBLE);
        presenter.getMeToOtherList(currentPage, SharePreferenceManager.getInstance().getAgentId(),time.getText().toString().replace("年", "-").replace("月", ""));
//        presenter.getMeToOtherList(currentPage, 5, time.getText().toString().replace("年", "-").replace("月", ""));
    }

    @Override
    public void getMeToOtherList(MeToOtherList meToOtherList) {
        loading.setVisibility(View.GONE);
        if (meToOtherList != null) {
            amount.setText(LZUtils.priceFormat(meToOtherList.getAmount()) + "元");
            if (meToOtherList.getRebateList() != null && meToOtherList.getRebateList().size() != 0) {
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage == 1) {
                    rebateList.clear();
                }
                rebateList.addAll(meToOtherList.getRebateList());
                adapter.notifyDataSetChanged();
            } else {
                if (currentPage == 1) {
                    emptyLayout.setVisibility(View.VISIBLE);
                } else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}
